package cn.ms.doctor.Controller;

import cn.ms.doctor.Entity.Drugs;
import cn.ms.doctor.Service.DrugsService;
import cn.ms.doctor.Utils.DataStandardUtils;
import cn.ms.doctor.Utils.GlobalMessage;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/Drugs")
public class DrugsController {

    @Resource
    private DrugsService drugsService;

    @RequestMapping("/isLogin/getDrugs")
    public Object getDrugs(@RequestBody Map<String,Object> params){
        if (DataStandardUtils.standardKeyIsEmpty(params,"pageIndex","pageSize")) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }

        return new GlobalMessage(200,0,null,drugsService.getDrugs(params));
    }

    @RequestMapping("/isLogin/isSystemAndDrugs/updateDrugsByDrugsId")
    public Object updateDrugsByDrugsId(@RequestBody Drugs drugs){

        if (StringUtils.isEmpty(drugs.getDrugsId())&&StringUtils.isEmpty(drugs.getDrugsAddress())
                ||StringUtils.isEmpty(drugs.getDrugsId())&&StringUtils.isEmpty(drugs.getDrugsCom())
                ||StringUtils.isEmpty(drugs.getDrugsId())&&StringUtils.isEmpty(drugs.getDrugsContent())
                ||StringUtils.isEmpty(drugs.getDrugsId())&&StringUtils.isEmpty(drugs.getDrugsCount())
                ||StringUtils.isEmpty(drugs.getDrugsId())&&StringUtils.isEmpty(drugs.getDrugsPrice())) {
            return new GlobalMessage(200,1,"非法请求...",null);
        }
        if (!drugsService.updateDrugsByDrugsId(drugs)){
            return new GlobalMessage(200,3,"异常问题,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"修改成功");

    }

    @RequestMapping("/isLogin/isSystemAndDrugs/deleteDrugsByDrugsId")
    public Object deleteDrugsByDrugsId(@RequestBody Drugs drugs){
        if (StringUtils.isEmpty(drugs.getDrugsId())) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }
        if (!drugsService.deleteDrugsByDrugsId(drugs)){
            return new GlobalMessage(200,3,"出现异常,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"删除成功");
    }
    @RequestMapping("/isLogin/isSystemAndDrugs/addDrugs")
    public Object addDrugs(@RequestBody Drugs drugs){
        if (StringUtils.isEmpty(drugs.getDrugsName())||StringUtils.isEmpty(drugs.getDrugsPrice())
            ||StringUtils.isEmpty(drugs.getDrugsCount())||StringUtils.isEmpty(drugs.getDrugsContent())
            ||StringUtils.isEmpty(drugs.getDrugsCom())||StringUtils.isEmpty(drugs.getDrugsAddress())) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }
        if (!drugsService.addDrugs(drugs)){
            return new GlobalMessage(200,3,"出现异常,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"添加成功");
    }

}
